/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.weather;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Parses Weather Underground XML quick and dirty.
 * 
 * @author Kyle O'Connor
 * 
 */
public class WeatherUndergroundXMLParser {

	private final static Logger log = Logger.getLogger(WeatherUndergroundXMLParser.class);

	private static final String ROOT_NODE = "location";
	private static final String STATE_NODE = "state";
	private static final String CITY_NODE = "city";
	private static final String ZIPCODE_NODE = "zip";

	/**
	 * Default constructor.
	 */
	public WeatherUndergroundXMLParser() {
		super();
	}

	public String parseZipCode(InputStream is) throws Exception {
		log.debug("Parsing Weather Underground Zip Code");
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(is);
		doc.getDocumentElement().normalize();

		// log.info("Root element " + doc.getDocumentElement().getNodeName());
		NodeList nodeLst = doc.getElementsByTagName(ROOT_NODE);
		// log.info("Information of WU location tag");

		Node fstNode = nodeLst.item(0);
		NodeList zstNm = null;

		if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
			Element fstElmnt = (Element) fstNode;
			NodeList zstNmElmntLst = fstElmnt.getElementsByTagName(ZIPCODE_NODE);
			Element zstNmElmnt = (Element) zstNmElmntLst.item(0);
			zstNm = zstNmElmnt.getChildNodes();
		}
		return ((Node) zstNm.item(0)).getNodeValue();
	}

	public String parseCityState(InputStream is) throws Exception {
		log.debug("Parsing Weather Underground City, State");
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(is);
		doc.getDocumentElement().normalize();

		// log.info("Root element " + doc.getDocumentElement().getNodeName());
		NodeList nodeLst = doc.getElementsByTagName(ROOT_NODE);
		// log.info("Information of WU location tag");

		for (int s = 0; s < nodeLst.getLength(); s++) {

			Node fstNode = nodeLst.item(s);

			if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element fstElmnt = (Element) fstNode;
				NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(STATE_NODE);
				Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
				NodeList fstNm = fstNmElmnt.getChildNodes();
				String state = ((Node) fstNm.item(0)).getNodeValue();
				NodeList lstNmElmntLst = fstElmnt.getElementsByTagName(CITY_NODE);
				Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
				NodeList lstNm = lstNmElmnt.getChildNodes();
				String city = ((Node) lstNm.item(0)).getNodeValue();
				return city + ", " + state;
			}

		}
		log.debug("Error must have occured because we are returning null");
		return null;
	}

	public static void main(String[] args) throws Exception {
		WeatherUndergroundXMLParser p = new WeatherUndergroundXMLParser();
		System.out.println(p.parseCityState(WeatherUndergroundHTTPRequest.get("39.959417", "-75.19483")));
		System.out.println(p.parseZipCode(WeatherUndergroundHTTPRequest.get("39.959417", "-75.19483")));
	}

}
